import Util from "./util";
import { APP_FUN } from "./enums";
/**
 * native2js
 * @param name
 * @param fn
 */
const _2js = (name, fn) => (window[name] = fn);

/**
 * js2native
 * @param name
 * @param param
 */
const _js2 = (name, param) => {
  param = param || "";
  param = Util.isString(param) ? param : JSON.stringify(param);
  try {
    // ios执行
    window.webkit.messageHandlers[name].postMessage(param);
  } catch (err) {
    // android执行
    try {
      window.JavaScriptInterface[name](param);
    } catch (e) {
      // 捕获不到执行
      console.warn("请在native环境下执行");
    }
  }
};

export default {
  _2js,
  _js2,
};
